package com.itheima.dianming.controller;

import com.baidu.aip.util.Base64Util;
import com.itheima.dianming.exception.BusinessException;
import com.itheima.dianming.filter.UserHolder;
import com.itheima.dianming.template.AliyunOssTemplate;
import com.itheima.dianming.template.BaiduAITemplate;
import com.itheima.dianming.pojo.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
public class UploadController {

    @Autowired
    private BaiduAITemplate baiduAITemplate;

    @Autowired
    private AliyunOssTemplate aliyunOssTemplate;

    @Value("${aliyun.oss.url}")
    private String ossUrl;

    @PostMapping("/upload")
    public R upload(MultipartFile file){
        try {
            if(file==null || file.isEmpty()){
                throw new BusinessException("文件不能为空");
            }
            String image = Base64Util.encode(file.getBytes());
            boolean b = baiduAITemplate.detectFace(image);
            if(!b){
                throw new BusinessException("未识别出人脸，请重写上传！");
            }
            String originalFilename = file.getOriginalFilename();
            String fileName = originalFilename.substring(originalFilename.lastIndexOf("."));
            fileName = UUID.randomUUID().toString()+fileName;
            boolean upload = aliyunOssTemplate.upload(file.getBytes(), fileName);
            if(!upload){
                throw new BusinessException("图片上传出错");
            }
            return R.data(ossUrl+fileName);
        } catch (IOException e) {
            e.printStackTrace();
            throw  new BusinessException("文件上传出错拉！");
        }
    }


    @PostMapping("/uploadQuestion")
    public R uploadQuestion(MultipartFile file){
        try {
            if(file==null || file.isEmpty()){
                throw new BusinessException("文件不能为空");
            }
            String originalFilename = file.getOriginalFilename();
            String fileName = originalFilename.substring(originalFilename.lastIndexOf("."));
            String username = UserHolder.getStu().getUsername();
            if(!"admin".equals(username)){
                if(!originalFilename.contains(username)){
                    throw new BusinessException("文件名必须包含自己的名称！");
                }
            }
            fileName = UUID.randomUUID().toString()+fileName;
            if(!fileName.endsWith("md")){
                throw new BusinessException("文件必须是markdown类型");
            }
            boolean upload = aliyunOssTemplate.upload(file.getBytes(), fileName);
            if(!upload){
                throw new BusinessException("文件上传出错");
            }
            return R.data(ossUrl+fileName);
        } catch (IOException e) {
            e.printStackTrace();
            throw  new BusinessException("文件上传出错拉！");
        }
    }
}
